#include "CWgPlotRef.h"
#include "ui_CWgPlotRef.h"
#include <algorithm>

CWgPlotRef::CWgPlotRef(REF_TYPE RefType, QWidget *parent)
	: QWidget(parent)
{
	ui = new Ui::CWgPlotRef();
	ui->setupUi(this);

	m_enRefType = RefType;
	initChart();
}

CWgPlotRef::~CWgPlotRef()
{
	delete ui;
}

void CWgPlotRef::initChart()
{
	ui->m_ChartView->setRenderHint(QPainter::Antialiasing);
	QChart* chart = ui->m_ChartView->chart();
	chart->legend()->hide();
	//Ref
	QLineSeries* sR = new QLineSeries();
	sR->setColor(Qt::green);
	chart->addSeries(sR);

	//Alarm1
	QLineSeries* sAlarm1 = new QLineSeries();
	sAlarm1->setColor(Qt::darkYellow);
	chart->addSeries(sAlarm1);

	// Alarm2
	QLineSeries* sAlarm2 = new QLineSeries();
	sAlarm2->setColor(Qt::darkYellow);
	chart->addSeries(sAlarm2);

	//Abort1
	QLineSeries* sAbort1 = new QLineSeries();
	sAbort1->setColor(Qt::red);
	chart->addSeries(sAbort1);

	// Abort2
	QLineSeries* sAbort2 = new QLineSeries();
	sAbort2->setColor(Qt::red);
	chart->addSeries(sAbort2);

	QAbstractAxis* axisX = nullptr;
	QAbstractAxis* axisY = nullptr;

	switch (m_enRefType)
	{
	case PSD:
		axisX = new QLogValueAxis();
		axisX->setTitleText("Frequency/(Hz)");
		dynamic_cast<QLogValueAxis*>(axisX)->setLabelFormat("%g");
		dynamic_cast<QLogValueAxis*>(axisX)->setBase(10.0);
		dynamic_cast<QLogValueAxis*>(axisX)->setMinorTickCount(-1);
		axisY = new QLogValueAxis();
		axisY->setTitleText("PSD/(g^2/Hz)");
		dynamic_cast<QLogValueAxis*>(axisY)->setBase(10.0);
		dynamic_cast<QLogValueAxis*>(axisY)->setMinorTickCount(-1);
		break;
	case COH:
		axisX = new QValueAxis();
		axisX->setTitleText("Frequency/(Hz)");
		axisY = new QValueAxis();
		axisY->setTitleText("CSD Coherence");
		break;
	case PHI:
		axisX = new QValueAxis();
		axisX->setTitleText("Frequency/(Hz)");
		axisY = new QValueAxis();
		axisY->setTitleText("CSD Phase");
		break;
	}

	chart->addAxis(axisX, Qt::AlignBottom);
	sR->attachAxis(axisX);
	sAlarm1->attachAxis(axisX);
	sAlarm2->attachAxis(axisX);
	sAbort1->attachAxis(axisX);
	sAbort2->attachAxis(axisX);

	chart->addAxis(axisY, Qt::AlignLeft);
	sR->attachAxis(axisY);
	sAlarm1->attachAxis(axisY);
	sAlarm2->attachAxis(axisY);
	sAbort1->attachAxis(axisY);
	sAbort2->attachAxis(axisY);

}


//
//void CWgPlotRef::RefreshChart(const std::vector<double>& x, const std::vector<double>& y)
//{
//	assert(x.size() == y.size());
//	QChart* chart = ui->m_ChartView->chart();
//
//	QVector<QPointF> vPoints;
//	for (int i = 0; i < x.size(); i++)
//	{
//		if (x[i] > 0 && y[i] > 0)
//		{
//			vPoints.push_back(QPointF(x[i], y[i]));
//		}
//	}
//
//	QLineSeries* series = new QLineSeries();
//	series->replace(vPoints);
//	chart->addSeries(series);
//	chart->legend()->hide();
//
//	QLogValueAxis *axisX = new QLogValueAxis();
//	axisX->setTitleText("Frequency/(Hz)");
//	axisX->setLabelFormat("%g");
//	axisX->setBase(10.0);
//	axisX->setMinorTickCount(-1);
//	axisX->setRange(vPoints.first().x()/10, vPoints.back().x()*10);
//	chart->addAxis(axisX, Qt::AlignBottom);
//	series->attachAxis(axisX);
//
//	QLogValueAxis *axisY = new QLogValueAxis();
//	axisY->setTitleText("PSD/(g^2/Hz)");
//	axisY->setLabelFormat("%g");
//	axisY->setBase(10.0);
//	axisY->setMinorTickCount(-1);
//	axisY->setRange(vPoints.first().y()/10, vPoints.back().y()*10);
//	chart->addAxis(axisY, Qt::AlignLeft);
//	series->attachAxis(axisY);
//
//}

void CWgPlotRef::RefreshChart(const REF_PSD & RefPSD)
{
	QChart* chart = ui->m_ChartView->chart();
	chart->legend()->hide();
	chart->setTitle(QString::fromStdString(RefPSD.szName));
	
	double* x = const_cast<double*>(RefPSD.vf.data());
	double* y = nullptr;
	double f1 = RefPSD.listSegment.front().f1;
	double df = RefPSD.listSegment.front().df;
	double f2 = RefPSD.listSegment.back().f2;
	int n1 = int(f1 / df);
	int n2 = int(f2 / df);

	QVector<QPointF> vPoints;
	auto listSeries = chart->series();
	//R
	vPoints.clear();
	y = const_cast<double*>(RefPSD.vRef.data());
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sR = dynamic_cast<QLineSeries*>(listSeries[0]);
	sR->replace(vPoints);

	//Alarm1
	vPoints.clear();
	y = const_cast<double*>(RefPSD.vAlarm1.data());
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sAlarm1 = dynamic_cast<QLineSeries*>(listSeries[1]);
	sAlarm1->replace(vPoints);

	// Alarm2
	vPoints.clear();
	y = (double*)RefPSD.vAlarm2.data();
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sAlarm2 = dynamic_cast<QLineSeries*>(listSeries[2]);
	sAlarm2->replace(vPoints);

	//Abort1
	vPoints.clear();
	y = (double*)RefPSD.vAbort1.data();
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sAbort1 = dynamic_cast<QLineSeries*>(listSeries[3]);
	sAbort1->replace(vPoints);

	// Abort2
	vPoints.clear();
	y = (double*)RefPSD.vAbort2.data();
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sAbort2 = dynamic_cast<QLineSeries*>(listSeries[4]);
	sAbort2->replace(vPoints);



	QLogValueAxis *axisX = dynamic_cast<QLogValueAxis*>(chart->axisX());
	axisX->setRange(vPoints.first().x() / 10, vPoints.back().x() * 10);

	QLogValueAxis *axisY = dynamic_cast<QLogValueAxis*>(chart->axisY());
	double ymin = sAbort1->points().back().y();
	double ymax = sAbort2->points().back().y();
	axisY->setRange(ymin / 10, ymax * 10);
}

void CWgPlotRef::RefreshChart(const REF_CSD & RefCSD)
{
	QChart* chart = ui->m_ChartView->chart();
	chart->legend()->hide();
	chart->setTitle(QString::fromStdString(RefCSD.szName));

	double* x = const_cast<double*>(RefCSD.vf.data());
	double* y = nullptr;
	double f1 = RefCSD.listSegment.front().f1;
	double df = RefCSD.listSegment.front().df;
	double f2 = RefCSD.listSegment.back().f2;
	int n1 = int(f1 / df);
	int n2 = int(f2 / df);

	QVector<QPointF> vPoints;
	auto listSeries = chart->series();
	//R
	vPoints.clear();
	y = const_cast<double*>(RefCSD.vRef.data());
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sR = dynamic_cast<QLineSeries*>(listSeries[0]);
	sR->replace(vPoints);

	//Alarm1
	vPoints.clear();
	y = const_cast<double*>(RefCSD.vAlarm1.data());
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sAlarm1 = dynamic_cast<QLineSeries*>(listSeries[1]);
	sAlarm1->replace(vPoints);

	// Alarm2
	vPoints.clear();
	y = (double*)RefCSD.vAlarm2.data();
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sAlarm2 = dynamic_cast<QLineSeries*>(listSeries[2]);
	sAlarm2->replace(vPoints);

	//Abort1
	vPoints.clear();
	y = (double*)RefCSD.vAbort1.data();
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sAbort1 = dynamic_cast<QLineSeries*>(listSeries[3]);
	sAbort1->replace(vPoints);

	// Abort2
	vPoints.clear();
	y = (double*)RefCSD.vAbort2.data();
	for (int i = n1; i <= n2; i++)
	{
		vPoints.push_back(QPointF(x[i], y[i]));
	}
	QLineSeries* sAbort2 = dynamic_cast<QLineSeries*>(listSeries[4]);
	sAbort2->replace(vPoints);

	// AxisX and AxisY
	QValueAxis *axisX = dynamic_cast<QValueAxis*>(chart->axisX());
	axisX->setRange(vPoints.first().x(), vPoints.back().x() );

	QValueAxis *axisY = dynamic_cast<QValueAxis*>(chart->axisY());
	double ymin = sAbort1->points().back().y();
	double ymax = sAbort2->points().back().y();
	axisY->setRange(ymin /*/ 10*/, ymax /** 10*/);
}
